﻿#include "ConnectionDialog.h"

ConnectionDialog::ConnectionDialog(QWidget *parent)
    : QDialog(parent),
      localConnectionRadioButton(new QRadioButton("本地连接")),
      remoteConnectionRadioButton(new QRadioButton("远程连接")),
      localDbNameEdit(new QLineEdit),
      hostEdit(new QLineEdit),
      portEdit(new QLineEdit),
      remoteDbNameEdit(new QLineEdit),
      usernameEdit(new QLineEdit),
      passwordEdit(new QLineEdit),
      localLabel(new QLabel("数据库名称")),
      remoteLabel1(new QLabel("主机")),
      remoteLabel2(new QLabel("端口")),
      remoteLabel3(new QLabel("数据库名称")),
      remoteLabel4(new QLabel("用户账号")),
      remoteLabel5(new QLabel("密码")),
      mainLayout(new QVBoxLayout),
      remoteLayout(new QVBoxLayout),
      localLayout(new QVBoxLayout),
      connectButton(new QPushButton("确定连接")),
      cancelButton(new QPushButton("取消"))
{
    setupUi();
    setupConnections();
    onConnectionTypeChanged();
}

void ConnectionDialog::setupUi() {
    setFixedWidth(600);  // 调整对话框的宽度

    setStyleSheet(
        "QLineEdit, QDateEdit {"
        "  font-size: 18px;"
        "  font-weight: bold;"
        "  border: 2px solid #E0E0E0;"
        "  border-radius: 20px;"
        "  padding: 10px;"
        "  color: gray;"
        "  width: 135%;"  // 增加编辑框的长度
        "}"
        "QPushButton {"
        "  font-size: 18px;"
        "  font-weight: bold;"
        "  border: 2px solid #E0E0E0;"
        "  border-radius: 20px;"
        "  padding: 10px;"
        "  background-color: #0093D1;"
        "  color: white;"
        "}");

    QButtonGroup *connectionTypeGroup = new QButtonGroup(this);
    connectionTypeGroup->addButton(localConnectionRadioButton);
    connectionTypeGroup->addButton(remoteConnectionRadioButton);

    QFormLayout *localFormLayout = new QFormLayout;
    localFormLayout->addRow(localLabel, localDbNameEdit);
    localLayout->addLayout(localFormLayout);

    QFormLayout *remoteFormLayout = new QFormLayout;

    remoteFormLayout->addRow(remoteLabel1, hostEdit);
    remoteFormLayout->addRow(remoteLabel2, portEdit);
    remoteFormLayout->addRow(remoteLabel3, remoteDbNameEdit);
    remoteFormLayout->addRow(remoteLabel4, usernameEdit);
    remoteFormLayout->addRow(remoteLabel5, passwordEdit);
    remoteLayout->addLayout(remoteFormLayout);

    mainLayout->addWidget(localConnectionRadioButton);
    mainLayout->addWidget(remoteConnectionRadioButton);
    mainLayout->addLayout(localLayout);
    mainLayout->addLayout(remoteLayout);

    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addStretch(1);
    buttonLayout->addWidget(connectButton);
    buttonLayout->addWidget(cancelButton);
    mainLayout->addLayout(buttonLayout);

    setLayout(mainLayout);

    // 设置默认选中“本地连接”
    localConnectionRadioButton->setChecked(true);
    onConnectionTypeChanged();
}

void ConnectionDialog::setupConnections() {
    connect(localConnectionRadioButton, &QRadioButton::toggled, this, &ConnectionDialog::onConnectionTypeChanged);
    connect(remoteConnectionRadioButton, &QRadioButton::toggled, this, &ConnectionDialog::onConnectionTypeChanged);
    connect(connectButton, &QPushButton::clicked, this, &ConnectionDialog::accept);
    connect(cancelButton, &QPushButton::clicked, this, &ConnectionDialog::reject);

    // 连接 QLineEdit 的 textChanged 信号到新的槽函数
    connect(localDbNameEdit, &QLineEdit::textChanged, this, &ConnectionDialog::onFieldsChanged);
    connect(hostEdit, &QLineEdit::textChanged, this, &ConnectionDialog::onFieldsChanged);
    connect(portEdit, &QLineEdit::textChanged, this, &ConnectionDialog::onFieldsChanged);
    connect(remoteDbNameEdit, &QLineEdit::textChanged, this, &ConnectionDialog::onFieldsChanged);
    connect(usernameEdit, &QLineEdit::textChanged, this, &ConnectionDialog::onFieldsChanged);
    connect(passwordEdit, &QLineEdit::textChanged, this, &ConnectionDialog::onFieldsChanged);
}

void ConnectionDialog::onConnectionTypeChanged() {
    bool isLocal = localConnectionRadioButton->isChecked();

    localLabel->setVisible(isLocal);
    localDbNameEdit->setVisible(isLocal);

    remoteLabel1->setVisible(!isLocal);
    hostEdit->setVisible(!isLocal);
    remoteLabel2->setVisible(!isLocal);
    portEdit->setVisible(!isLocal);
    remoteLabel3->setVisible(!isLocal);
    remoteDbNameEdit->setVisible(!isLocal);
    remoteLabel4->setVisible(!isLocal);
    usernameEdit->setVisible(!isLocal);
    remoteLabel5->setVisible(!isLocal);
    passwordEdit->setVisible(!isLocal);

    // 检查输入字段是否已填写
    onFieldsChanged();
}

void ConnectionDialog::onFieldsChanged() {
    bool enableButton = false;
    bool isFilled = false;

    if (localConnectionRadioButton->isChecked()) {
        enableButton = !localDbNameEdit->text().isEmpty();
        isFilled = enableButton;
    } else if (remoteConnectionRadioButton->isChecked()) {
        enableButton = !hostEdit->text().isEmpty() && !portEdit->text().isEmpty() &&
                       !remoteDbNameEdit->text().isEmpty() && !usernameEdit->text().isEmpty() &&
                       !passwordEdit->text().isEmpty();
        isFilled = enableButton;
    }

    connectButton->setEnabled(enableButton);
    emit connectionStatusChanged(isFilled);  // 发出信号
}

QString ConnectionDialog::getLocalDbName() const
{
    return localDbNameEdit->text();
}

QString ConnectionDialog::getHost() const
{
    return hostEdit->text();
}

QString ConnectionDialog::getPort() const
{
    return portEdit->text();
}

QString ConnectionDialog::getRemoteDbName() const
{
    return remoteDbNameEdit->text();
}

QString ConnectionDialog::getUsername() const
{
    return usernameEdit->text();
}

QString ConnectionDialog::getPassword() const
{
    return passwordEdit->text();
}
